package com.ojoin.ojoin.eb_spider.service.stat;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.utils.WebServiceUtils;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotal;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;
@Service
public class SupplierYHDstat extends BaseStatTotal {

	private Logger log = LoggerFactory.getLogger(SupplierYHDstat.class);

	@Override
	public void invoke(TbSlPlatformUser user, TbCatchdataStrategy ca,TbSlLoanStatTotal total)
			throws Exception {

		String stime = WebServiceUtils.getPaymentLastsettlementDate(
				user.getId()).replace("/", "-");

		if (CommonUtils.isEmpty(stime)) {
			stime = WebServiceUtils.getPaymentLastInsertTime(user.getId());
		}

		String etime = TimeUtils.getCurDate();

		List<String> unpayIds = new ArrayList<String>();

		final int CLEARINGSCYCLE = 48;

		final int[] clearingsDay = { 6, 16, 26 };

		long paymoney = 0;

		List<TbSlSupplyPayment> waitpays = WebServiceUtils
				.getSupplyPaymentByplateformId(user.getId(),ca, null, null,
						TbSlSupplyPayment.WAITPAY);

		Iterator<TbSlSupplyPayment> it = waitpays.iterator();
		while (it.hasNext()) {
			TbSlSupplyPayment tb = it.next();

			String strday;
			String needtime;
			try {
				needtime = TimeUtils.getDateAfterDays(tb.getCreateDate(),
						CLEARINGSCYCLE);

				int day = Integer.valueOf(TimeUtils.getCurDay(needtime
						.substring(0, 10)));

				if (clearingsDay[0] <= day && day < clearingsDay[1]) {
					strday = String.valueOf(clearingsDay[1]);
				} else if (clearingsDay[1] <= day && day < clearingsDay[2]) {
					strday = String.valueOf(clearingsDay[2]);
				} else {
					strday = "0" + String.valueOf(clearingsDay[0]);
				}

				needtime = needtime.substring(0, 8) + strday;

				if (TimeUtils.getDateSpaceOfDay(needtime,
						TimeUtils.getCurDate()) <= 0) {
					unpayIds.add(tb.getSettlementId());
					paymoney = paymoney + tb.getAmount();
				}

			} catch (ParseException e) {
				LogUtils.error(log, "时间转化出错" + tb.getCreateDate());
			} catch (Exception e) {
				LogUtils.error(log, "时间转化出错" + tb.getCreateDate());
			}
		}

		long inveInMoney = WebServiceUtils
				.getSupplyInventoryMoneyByplateformId(user.getId(), ca,stime,
						etime, TbSlSupplyInventory.IN);

		long inveOutMoney = WebServiceUtils
				.getSupplyInventoryMoneyByplateformId(user.getId(),ca, stime,
						etime, TbSlSupplyInventory.OUT);

		total.setTotalUnsettleInstoreorder(Long.valueOf(inveInMoney));
		total.setTotalUnenterinRefundorder(Long.valueOf(inveOutMoney));
		total.setTotalUnpaySettleorder(Long.valueOf(paymoney));

		LogUtils.info(
				log,
				"%s --> [ store in stime:%s--etime:%s] + [store out stime:%s--etime:%s] + [ Unpay : %s] ",
				user.getUsername(), stime, etime, stime, etime,
				JSONUtils.toJSONString(unpayIds));

	}

}
